package com.fanqwz.hexieshe.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.fanqwz.hexieshe.Navigator;
import com.fanqwz.hexieshe.R;
import com.fanqwz.hexieshe.activity.BaseActivity;
import com.fanqwz.hexieshe.adapter.CategoryAdapter;
import com.fanqwz.hexieshe.model.CategoryModel;
import com.fanqwz.hexieshe.presenter.CategoryPresenter;
import com.fanqwz.hexieshe.presenter.HeadLinePresenter;
import com.fanqwz.hexieshe.view.CategoryView;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by fanqw on 2017/7/2.
 */

public class CategoryFragment extends BaseFragment implements CategoryView, AdapterView.OnItemClickListener {
    @Nullable
    DrawerLayout drawerLayout;

    @Bind(R.id.listview)
    ListView listView;
    @Bind(R.id.progress_bar)
    ProgressBar progressBar;

    @Inject
    CategoryPresenter presenter;
    @Inject
    CategoryAdapter categoryAdapter;
    @Inject
    HeadLinePresenter headLinePresenter;
    @Inject
    ActionBar actionBar;
    @Inject
    Navigator navigator;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((BaseActivity) getActivity()).getComponent().inject(this);
        presenter.setCategoryView(this);
        presenter.create();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.category_list, container, false);
        ButterKnife.bind(this, root);
        listView.setAdapter(categoryAdapter);
        listView.setOnItemClickListener(this);
        presenter.show();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        presenter.hide();
        ButterKnife.unbind(this);
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        presenter.destroy();
        super.onDestroy();
    }

    @Override
    public void prepare() {
        progressBar.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
    }

    @Override
    public void showCategorys(List<CategoryModel> models) {
        progressBar.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
        categoryAdapter.setCategorys(models);
        categoryAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        headLinePresenter.setCategory(((CategoryModel) categoryAdapter.getItem(position)).getName());
        headLinePresenter.setCategoryDisplayName(((CategoryModel) categoryAdapter.getItem(position)).getDisplayName());
        headLinePresenter.setTag(null);
        navigator.showMainPage();
        headLinePresenter.show();

        actionBar.setTitle(((CategoryModel) categoryAdapter.getItem(position)).getDisplayName());

        if (drawerLayout != null)
            drawerLayout.closeDrawers();
    }

    public void setDrawerLayout(DrawerLayout drawerLayout) {
        this.drawerLayout = drawerLayout;
    }
}
